<?php
/**
 * Guestbook
 *
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
/**
 * Guestbook
 *
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
/**
 * Guestbook
 *
 * @package
 * @author kubicek
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class Guestbook extends Plugin
{
	public $plugin_name = "Guestbook"; //name of plugin
	public $plugin_short_desc = "Simple guestbook"; //short description of plugin
	public $plugin_long_desc = "Simple guestbook with option of authorization"; //longer description - what does plugin do
	public $version = "1.0"; //version of plugin
	public $has_sidebar = false; //does this plugin have sidebar -> function generate_sidebar()
	public $has_footer = false; //does this plugin have footer -> function generate_footer()
	public $needs_install = true;

	/**
	 * Constructor
	 */
	function __construct()
	{
		global $theme;
		$theme->set_title('Guestbook'); //set page <title>

		// template with comment loop
		define('FILE_COMMENT_THEME', 'comment.html');

		// template with guestbook
		define('FILE_GUESTBOOK_THEME', 'guestbook.html');
	}

	/**
	 * Guestbook::start()
	 *
	 * @return
	 */
	public function start()
	{
		$theme = new Theme();

		$theme->set_theme_file('',FILE_GUESTBOOK_THEME);
		$theme->assign('comments',$this->get_comments());
		echo $theme->get_content();
	}

	/**
	 * Blog::get_comments()
	 * Return string with formated comments according to selected theme and article id
	 * @param mixed $id
	 * @return mixed $out string with formated comments text
	 */
	private function get_comments()
	{
		global $core;

		$out = '';

		$query = mysql_query("SELECT * FROM yaps_guestbook WHERE auth = 1 ORDER BY posted DESC");
		if(mysql_num_rows($query) > 0)
		{
			$theme = new Theme();
			$theme->set_theme_file('',FILE_COMMENT_THEME);

			//try to get Gravatar
			if($core->is_plugin('gravatar'))
			{
				$gravatar = $core->load_plugin('gravatar');
				$gravatar_plugin = true;
			}

			while($comment = mysql_fetch_array($query))
			{
				//do ve have gravatar and email is longer than nothing?
				if($gravatar_plugin == true && strlen($comment['email']) > 0)
				{
					$username = $gravatar->get_gravatar($comment['email'],'32').$comment['name'];
				}
				else
				{
					$username = $comment['name'];
				}

				$theme->purge();
				$theme->assign('user',$username);
				$theme->assign('posted',$core->convert_datetime($comment['posted']));
				$theme->assign('post',$comment['text']);
				$out .= $theme->get_content(); //get content of theme with assigns WITHOUT rendering it
			}
		}
		else
		{
			$out = 'No comments yet. Be the first one who writes a comment.';
		}

		return $out;
	}

	/**
	 * Guestbook::post_comment()
	 *
	 * @return
	 */
	public function post_comment()
	{
		global $core;

		$name = $core->post_hsc('name');
		$email = $core->post_hsc('email');
		$text = $core->post_hsc('comment');
		$ip = $core->get_ip();

		if(strlen($name) > 0 && strlen($text) > 0)
		{
			$query = mysql_query("INSERT INTO yaps_guestbook VALUES('0','".$name."','".$email."','".$text."',NOW(),'".$ip."',".GUESTBOOK_AUTH_MODE.")");
			if(!$query)
			{
				$core->err('Error: Sorry i can`t insert your comment. Contact admin please.');
			}
			else
			{
				$core->msg('Success: Your comment has been posted.');
			}
		}

		$core->refresh(WEB_URL.'guestbook');
	}

	private function handle_get_stuff()
	{
		global $core;

		if($core->get('delete'))
		{
			$query = mysql_query("DELETE FROM yaps_guestbook WHERE id = ".$core->get('delete'));

			$core->msg("Comment has been deleted!");
			$core->refresh_js(WEB_URL.'admin-pluginsettings?plugin=guestbook');
		}
		elseif($core->get('auth'))
		{
			$post = mysql_fetch_array(mysql_query("SELECT * FROM yaps_guestbook WHERE id = ".$core->get('auth')));

			if($post['auth'] == 1) mysql_query("UPDATE yaps_guestbook SET auth = 0 WHERE id = ".$post['id']);
			elseif($post['auth'] == 0) mysql_query("UPDATE yaps_guestbook SET auth = 1 WHERE id = ".$post['id']);

			$core->msg("Comment has been authorized / deauthorized.");
			$core->refresh_js(WEB_URL.'admin-pluginsettings?plugin=guestbook');
		}

	}

	/**
	 * Guestbook::admin_settings()
	 * Administration settings of plugin, if you can`t administrate anything it`s good to place some example of usage here
	 * It will be printed in administration of Yaps!
	 * @return
	 */
	public function admin_settings()
	{
		$this->handle_get_stuff();

		$query = mysql_query("SELECT * FROM yaps_guestbook WHERE auth = 0");
		if(mysql_num_rows($query) > 0)
		{
			echo '<h3>Non-authorized comments</h3>
			<table>';

			while($row = mysql_fetch_array($query))
			{
				echo '<tr>
								<td>'.$row['name'].'</td>
								<td>'.$row['text'].'</td>
								<td><a href="admin-pluginsettings?plugin=guestbook&amp;delete='.$row['id'].'">X</a>
										<a href="admin-pluginsettings?plugin=guestbook&amp;auth='.$row['id'].'">A</a></td>
							</tr>';
			}

			echo '</table>';
		}

		$query = mysql_query("SELECT * FROM yaps_guestbook WHERE auth = 1");
		if(mysql_num_rows($query) > 0)
		{
			echo '<h3>Authorized comments</h3>
			<table>';

			while($row = mysql_fetch_array($query))
			{
				echo '<tr>
								<td>'.$row['name'].'</td>
								<td>'.$row['text'].'</td>
								<td><a href="admin-pluginsettings?plugin=guestbook&amp;delete='.$row['id'].'">X</a>
										<a href="admin-pluginsettings?plugin=guestbook&amp;auth='.$row['id'].'">A</a></td>
							</tr>';
			}

			echo '</table>';
		}
	}

	/**
	 * Guestbook::install()
	 * Install script
	 * @return
	 */
	public function install()
	{
		$create_table = mysql_query("CREATE TABLE IF NOT EXISTS `yaps_guestbook` (
		   `id` int(11) NOT NULL AUTO_INCREMENT,
		   `name` text COLLATE utf8_czech_ci NOT NULL,
		   `email` text COLLATE utf8_czech_ci NOT NULL,
		   `text` text COLLATE utf8_czech_ci NOT NULL,
		   `posted` datetime NOT NULL,
		   `ip` varchar(15) COLLATE utf8_czech_ci NOT NULL,
		   `auth` int(1) NOT NULL,
		   PRIMARY KEY (`id`)
		   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1");

		$insert_data_1 = mysql_query('INSERT INTO yaps_config VALUES(\'guestbook_auth_mode\',\'0\')');
		$insert_data_2 = mysql_query('INSERT INTO yaps_config VALUES(\'plugin_guestbook_installed\',\'true\')');

		if($create_table && $insert_data_1 && $insert_data_2)
		{
			return true;
		}
		else return false;
	}
}


?>